package ch.bfh.mobicompsoda2.sms_sender;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import ch.bfh.mobicompsoda2.sms_sender.sender_service.SODA_Send_SMS_Service;

public class SenderActivity extends Activity {

    private static final String TAG = SenderActivity.class.getName();

    private Switch sendPossibleSwitch = null;
    private TextView numberTextView = null;
    private TextView sharedPreferenceTextView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sender);
        sendPossibleSwitch = (Switch) findViewById(R.id.sw_activity_sender_sendpossible);
        numberTextView = (TextView) findViewById(R.id.et_activity_sender_phonenumber);
        sharedPreferenceTextView = (TextView) findViewById(R.id.et_activity_sender_sharedpreference_name);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sender, menu);
        return true;
    }

    public void onButtonClick(View view) {
        switch (view.getId()) {
        case R.id.bu_activity_sender_sendsms:
            try {

                SharedPreferences tsp = getSharedPreferences("TEST", MODE_PRIVATE);
                Editor editor = tsp.edit();
                editor.putString("MYTEST", "bubu");
                editor.commit();

                // this is just a mess
                String number = numberTextView.getText().toString();
                String moduleTag = "ModuleTag";
                String sharedPreferences = sharedPreferenceTextView.getText().toString();

                // This is the real shit
                Intent sendIntent = SODA_Send_SMS_Service.getSendSMSIntent(number, moduleTag, sharedPreferences);
                sendBroadcast(sendIntent);

            } catch (Exception e) {
                Log.e(TAG, "ERROR: send Broadcast");
                Toast.makeText(this, "ERROR: send Broadcast", Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }
}
